package com.lxu.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

/**
 * 一次性扩展多行 存在样式丢失问题
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
public class WordComplex5 {

    @Test
    public void test() throws Exception {
        String path = "D:\\ComplexWord.docx";
        FileInputStream in = new FileInputStream(path);
        XWPFDocument hwpf = new XWPFDocument(in);
        List<XWPFTable> tables = hwpf.getTables();//获取word中所有的表格
        FileOutputStream out = new FileOutputStream("D:\\ComplexWord2.docx");
        XWPFTable table = tables.get(0);//获取第一个表格
        for (int i = 1; i <= 3; i++) {
            copy(table, i, i + 3, 1);
        }

        hwpf.write(out);
        out.flush();
        out.close();
    }

    public void copy(XWPFTable table, int sourceIndex, int rowIndex, int index) {
        XWPFTableRow sourceRow = table.getRow(sourceIndex);
        //在表格指定位置新增一行
        XWPFTableRow targetRow = table.insertNewTableRow(rowIndex);
        //复制行属性
        targetRow.getCtRow().setTrPr(sourceRow.getCtRow().getTrPr());
        List<XWPFTableCell> cellList = sourceRow.getTableCells();
        if (null == cellList) {
            return;
        }
        //复制列及其属性和内容
        XWPFTableCell targetCell = null;
        for (XWPFTableCell sourceCell : cellList) {
            targetCell = targetRow.addNewTableCell();
            //列属性
            targetCell.getCTTc().setTcPr(sourceCell.getCTTc().getTcPr());
            //段落属性
            if (sourceCell.getParagraphs() != null && sourceCell.getParagraphs().size() > 0) {
                targetCell.getParagraphs().get(0).getCTP().setPPr(sourceCell.getParagraphs().get(0).getCTP().getPPr());
                if (sourceCell.getParagraphs().get(0).getRuns() != null && sourceCell.getParagraphs().get(0).getRuns().size() > 0) {
                    XWPFRun cellR = targetCell.getParagraphs().get(0).createRun();
                    //cellR.setText(sourceCell.getText());
                    setCellText(sourceCell, targetCell, index);
                    cellR.setBold(sourceCell.getParagraphs().get(0).getRuns().get(0).isBold());
                } else {
                    setCellText(sourceCell, targetCell, index);
                    //targetCell.setText(sourceCell.getText());
                }
            } else {
                setCellText(sourceCell, targetCell, index);
                //targetCell.setText(sourceCell.getText());
            }
        }
    }

    private void setCellText(XWPFTableCell sourceCell, XWPFTableCell targetCell, int index) {
        String text = sourceCell.getText();
        if (StringUtils.isNotBlank(text)) {
            int begin = text.trim().indexOf("${") + 2;
            int end = text.trim().indexOf("}");
            if (begin >= 0 && end >= 0) {
                StringBuffer sb = new StringBuffer().append("{").append(text.trim().substring(begin, end)).append(index).append("}");
                targetCell.setText(sb.toString());
            } else {
                targetCell.setText(text);
            }
        }
    }

}
